app.controller('NewUnitModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$localStorage', '$state', '$location',
        function ($scope, $modal, $log, $http, $window, $localStorage, $state, $location) {
            $scope.items = ['item1', 'item2', 'item3'];
            //console.log($state.params);
            var school_id, campus_building_id, layer_id,room_name;
            var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
                $scope.oo = {};
                $scope.mmData = $localStorage.layers_info;
                $scope.items = items;
                $scope.selected = {
                    item: $scope.items[0]
                };
                $scope.ok = function () {
                    school_id = $scope.oo.buildingtype.split('+')[1];
                    campus_building_id = $scope.oo.layer_name.split('+')[1];
                    layer_id = $scope.oo.layer_name.split('+')[2];
                    room_name = $scope.oo.unitname;
                    $modalInstance.close($scope.selected.item);
                };

                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            };
            $scope.open = function (size) {
                var modalInstance = $modal.open({
                    templateUrl: 'newunitModalContent.html',
                    controller: ModalInstanceCtrl,
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;
                        }
                    }
                });
                modalInstance.result.then(function (selectedItem) {
                    
                    $http.post(
                        $scope.app.backendurl+"tbl-room/create",
                        {
                            'school_id': school_id,
                            'campus_building_id': campus_building_id,
                            'layer_id': layer_id,    
                            'room_name':room_name,
                            'token':$localStorage.token                   

                        }
                    ).success(function (data) {
                     //console.log(data);
                      if(data.code==0&&data.msg=='创建成功'){
                        alert(data.msg);
                        $window.location.reload('app/sys/unit');                      

                      }
                      if(data.code==1){
                        alert(data.msg)
                      }
                      if(data.code==20001){
                        $location.path('access.signin');
                      }
                    }).error(function (data) {
                        $scope.authError = data.error.code;
                        if ($scope.authError == 401) {
                            $location.path('access.signin');
                        }
                    })
                });
            };
        }])

app.controller('AddManagerModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$localStorage', '$state', '$location',
        function ($scope, $modal, $log, $http, $window, $localStorage, $state, $location) {
            $scope.items = ['item1', 'item2', 'item3'];
            //console.log($state.params);
       
            var school_id, room_id, admin_id,version;
            var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
                $scope.oo = {};
                $scope.items = items;
                $scope.selected = {
                    item: $scope.items[0]
                };

                $http.post(
                    "http://118.178.136.209/index.php?r=tbl-room/get-admins",
                    {
                        'school_id': school_id, 
                        'room_id': room_id,
                        'token':$localStorage.token   

                    }
                ).success(function (data) {
                   //console.log(data);
                  if(data.code==0&&data.msg=='读取成功'){                    
                    $scope.oo.managerdata = data.data.list;
                    $scope.oo.version = data.data.version;                     

                  }
                  if(data.code==1){
                    alert(data.msg)
                  }
                  if(data.code==20001){
                    $location.path('access.signin');
                  }
                }).error(function (data) {
                    $scope.authError = data.error.code;
                    if ($scope.authError == 401) {
                        $location.path('access.signin');
                    }
                })
                $scope.ok = function () {
                    admin_id = $scope.oo.manager.split(",")[0];
                    version = $scope.oo.version;
                    $modalInstance.close($scope.selected.item);
                };

                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            };
            $scope.addmanager = function(rid,sid){                
                school_id = sid;
                room_id = rid;
                //console.log(school_id)
                $scope.open();
            }
            $scope.open = function (size) {
                var modalInstance = $modal.open({
                    templateUrl: 'addmanagerModalContent.html',
                    controller: ModalInstanceCtrl,
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;
                        }
                    }
                });
                modalInstance.result.then(function (selectedItem) {                    
                    $http.post(
                      $scope.app.backendurl+"tbl-room/update-admin",
                        {
                            'school_id': school_id,
                            'room_id': room_id,
                            'admin_id': admin_id,    
                            'version': version,
                            'token':$localStorage.token                   

                        }
                    ).success(function (data) {
                     //console.log(data);
                      if(data.code==0&&data.msg=='修改成功'){
                        alert(data.msg);
                        $window.location.reload('app/sys/unit'); 
                      }
                      if(data.code==1){
                        alert(data.msg)
                      }
                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        $scope.authError = data.error.code;
                        if ($scope.authError == 401) {
                            $location.path('access.signin');
                        }
                    })
                });
            };
        }])

app.controller('AddStudentModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$localStorage', '$state', '$location',
        function ($scope, $modal, $log, $http, $window, $localStorage, $state, $location) {
            $scope.items = ['item1', 'item2', 'item3'];
            //console.log($state.params);
            var school_id, personnel_number, room_id,room_member={};
            room_member.room_leader = []; room_member.room_members = [];
            var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
                $scope.oo = {};
                $scope.items = items;
                $scope.selected = {
                    item: $scope.items[0]
                };
                // $scope.getroomstudent = function(){
                    $scope.inputitems=[];  
                    var i;

                    $http.post(
                       "http://118.178.136.209/index.php?r=tbl-room/get-room-members",
                        {
                            'school_id': school_id,
                            'room_id': room_id,                        
                            'token':$localStorage.token                   

                        }
                    ).success(function (data) {
                     //console.log(data)
                      if(data.code==0&&data.msg=='读取成功'){
                        if(data.data.room_member.length!=0){
                            if(data.data.room_member.room_leader.length!=0){
                                $scope.oo.personnel_number = data.data.room_member.room_leader[0].personnel_number;
                                $scope.oo.name = data.data.room_member.room_leader[0].name;
                                $scope.oo.phone = data.data.room_member.room_leader[0].phone;
                                $scope.oo.pid = data.data.room_member.room_leader[0].id;
                            }
                            $scope.inputitems = data.data.room_member.room_members;
                            
                        }                     
                        
                        
                       
                      }
                      if(data.code==1){                        
                        $scope.oo.info = data.msg;
                        $scope.oo.name = data.msg;
                        $scope.oo.phone = '';
                      }
                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        $scope.authError = data.error.code;
                        if ($scope.authError == 401) {
                            $location.path('access.signin');
                        }
                    })
                // }
                $scope.getpersoninfo = function(){                   
                    $http.post(
                       "http://118.178.136.209/index.php?r=tbl-room/get-personnel-info",
                        {
                            'school_id': school_id,
                            'personnel_number': $scope.oo.personnel_number,                        
                            'token':$localStorage.token                   

                        }
                    ).success(function (data) {                     
                      if(data.code==0&&data.msg=='读取成功'){
                        $scope.oo.name = data.data.name;
                        $scope.oo.phone = data.data.phone;
                        $scope.oo.pid = data.data.id;
                       
                      }
                      if(data.code==1){                        
                        $scope.oo.info = data.msg;
                        $scope.oo.name = data.msg;
                        $scope.oo.phone = '';
                      }
                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        $scope.authError = data.error.code;
                        if ($scope.authError == 401) {
                            $location.path('access.signin');
                        }
                    })
                }

                
                               
                $scope.getResult=function(key){
                    var result;                    
                    result = $scope.inputitems[key];                   
                    $http.post(
                       "http://118.178.136.209/index.php?r=tbl-room/get-personnel-info",
                        {
                            'school_id': school_id,
                            'personnel_number': result.personnel_number,                        
                            'token':$localStorage.token                   

                        }
                    ).success(function (data) {   
                    //console.log(data)                  
                      if(data.code==0&&data.msg=='读取成功'){
                        $scope.inputitems[key].id = data.data.id;
                        $scope.inputitems[key].name = data.data.name;
                        $scope.inputitems[key].phone = data.data.phone;
                       
                      }
                      if(data.code==1){
                        // $scope.oo.pid = data.id;
                        $scope.oo.info = data.msg;
                        $scope.inputitems[key].name = data.msg,
                        $scope.inputitems[key].phone = '';
                      }
                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        $scope.authError = data.error.code;
                        if ($scope.authError == 401) {
                            $location.path('access.signin');
                        }
                    })
                  


                }

                $scope.Fn= {

                    add: function () {   
                        i = $scope.inputitems.length;                        
                        $scope.inputitems[i] = {};

                        i++;
                    },
                    del: function (key) {     
                       i = $scope.inputitems.length;  
                        $scope.inputitems.splice(key, 1);
                        i--;
                          
                    }
                }               
               

                $scope.ok = function () {
                    var roomleader = {};  
                    var roomleaders = [];
                    roomleader.id = $scope.oo.pid;
                    roomleaders.push(roomleader);
                    room_member.room_leader = roomleaders;   
                    var roommembers = [];
                    for(var i = 0;i<$scope.inputitems.length;i++){
                         var roommember = {};
                        for(var key in $scope.inputitems[i]){                          
                            if(key=="id"){
                                roommember.id = $scope.inputitems[i][key]
                            }                             
                        }
                        
                        roommembers.push(roommember);                          
                    } 
                    room_member.room_members = roommembers;                 
                                      
                    $modalInstance.close($scope.selected.item);
                };

                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            };
            $scope.addstudent = function(rid,sid){
                school_id = sid;
                room_id = rid;
                
                $scope.open('lg');

            }
            $scope.open = function (size) {
                var modalInstance = $modal.open({
                    templateUrl: 'addstudentModalContent.html',
                    controller: ModalInstanceCtrl,
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;
                        }
                    }
                });
                modalInstance.result.then(function (selectedItem) {
                    //console.log(room_member);
                    $http.post(
                        $scope.app.backendurl+"tbl-room/update-room-members",
                        {
                            'school_id': school_id,
                            'room_id': room_id,
                            'room_member': JSON.stringify(room_member),      
                            'token':$localStorage.token                   

                        }
                    ).success(function (data) {
                      //console.log(data);
                      if(data.code==0&&data.msg=="修改成功"){
                        alert(data.msg);
                        $window.location.reload('app/sys/unit');                      

                      }
                      if(data.code==1){
                        alert(data.msg)
                      }
                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        $scope.authError = data.error.code;
                        if ($scope.authError == 401) {
                            $location.path('access.signin');
                        }
                    })
                });
            };
        }])



app.controller('DelUnitModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$location','$localStorage',
        function ($scope, $modal, $log, $http, $window, $location,$localStorage) {

            $scope.items = ['item1', 'item2', 'item3'];
            var unit_id,school_id;            
            var ModalInstanceCtrl = function ($scope, $modalInstance, items) {

                $scope.oo = {};

                $scope.items = items;               
                $scope.selected = {
                    item: $scope.items[0]
                };
                $scope.ok = function () {

                    $modalInstance.close($scope.selected.item);
                };

                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            };


            $scope.delunit = function (rid,sid) {
                //console.log(aid);
                unit_id = rid;   
                school_id  = sid;        
                $scope.open();
            }

            $scope.open = function (size) {

                var modalInstance = $modal.open({
                    templateUrl: 'delunitModalContent.html',
                    controller: ModalInstanceCtrl,
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;

                        }
                    }
                });

                modalInstance.result.then(function (selectedItem) {
                    $http.post(
                        $scope.app.backendurl+"tbl-room/delete",
                        {
                           
                            'school_id': school_id,  
                            'room_id': unit_id,                           
                            'token':$localStorage.token                   

                        }
                    ).success(function (data) {
                      //console.log(data);
                      if(data.code==0&&data.msg=="删除成功"){
                        alert(data.msg);
                        $window.location.reload('app/sys/campus');

                      }
                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        $scope.authError = data.error.code;
                        if ($scope.authError == 401) {
                            $location.path('access.signin');
                        }
                    })


                }, function () {
                    
                });
            };
        }])